<template>
	<view>
		<!--搜索组件-->
		<my-search @click = "gotoSearch"></my-search>
		<view class="scroll-view-container">
			<!--左侧滑动区域-->
			<scroll-view class="left-scroll" scroll-y="true" :style="{height:windowHeight + 'px'}">
				<block v-for="(item,i) in cateList" :key="i">
					<view @click="handleClick(i)" :class="['left-scroll-item',i === active ? 'active' : '']">{{item.cat_name}}</view>
				</block>
				
			</scroll-view>
			<!--右侧滑动区域-->
			<scroll-view :scroll-top="scrollTop" class="right-scroll" scroll-y="true" :style="{height:windowHeight + 'px'}">
				<view v-for="(item2,i2) in cateLevel2" :key="i2">
					<view class="right-scroll-title">
						/{{item2.cat_name}}/
					</view>
					<view class="level3-list">
						<view @click="gotogoodsList(item3)" class="level3-item" v-for="(item3,i3) in item2.children" :key="i3">
							<image class="level3-img" :src="item3.cat_icon" mode=""></image>
							<text>{{item3.cat_name}}</text>
						</view>
					</view>
				</view>
			</scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				windowHeight:0,
				//分类列表数据
				cateList:[],
				active:0,
				//二级分类的列表
				cateLevel2:[],
				//控制滚动条在刷新后自动回到顶部
				scrollTop:0,
			};
		},
		onLoad() {
			const sysInfo = uni.getSystemInfoSync()
			this.windowHeight = sysInfo.windowHeight
			
			this.getcateList()
		},
		methods:{
			async getcateList(){
				const {data:res} = await uni.$http.get('/api/public/v1/categories')
				if(res.meta.status !== 200){
					return uni.$showMsg()
				}
				this.cateList = res.message
				//为二级分类赋值
				this.cateLevel2 = res.message[0].children
			},
			handleClick(i){
				this.active = i
				//重新为二级分类赋值
				this.cateLevel2 = this.cateList[i].children
				
				this.scrollTop = this.scrollTop === 0 ? 1 : 0
			},
			gotogoodsList(item3){
				uni.navigateTo({
					url:'/subpkg/goods_list/goods_list?cid=' + item3.cat_id
				})
			},
			gotoSearch(){
				uni.navigateTo({
					url:'/subpkg/search/search'
				})
			}
		}
	}
</script>

<style lang="scss">
.scroll-view-container{
	display: flex;
}
.left-scroll{
	width: 120px;
}
.left-scroll-item{
	background-color: #F7F7F7;
	line-height: 60px;
	text-align: center;
	font-size: 12px;
	&.active{
		background-color: #FFFFFF;
		position: relative;
		&::before{
			content: ' ';
			display: block;
			width: 3px;
			height: 30px;
			position: absolute;
			background-color: #c00000;
			top: 50%;
			left: 0;
			transform: translateY(-50%);
		}
	}	
}
.right-scroll-title{
		font-size: 12px;
		font-weight: bold;
		text-align: center;
		padding: 15px 0;
}
.level3-list{
	display: flex;
	flex-wrap: wrap;
	.level3-item{
		width: 33.3%;
		display: flex;
		flex-direction: column;
		justify-content: center;
		align-items: center;
		margin-bottom: 10px;
		image{
			width: 60px;
			height: 60px;
		}
		text{
			font-size: 12px;
		}
	}
}
</style>
